#pragma once
#include <iostream>
#include <string>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "log.hpp"

using namespace std;

const char *pathname = "/home/ljy/";
const int proj_id = 0x6666;
const size_t sharemem_size = 4096;

key_t GetKey()
{
  key_t k = ftok(pathname, proj_id);
  if (k < 0)
  {
    perror("GetKey::ftok fail");
    exit(1);
  }
  return k;
}

int GetShareMemHelper(int flag)
{
  key_t k = GetKey();
  int shmid = shmget(k, sharemem_size, flag);
  if (shmid < 0)
  {
    perror("GetShareMemHelper shmget fail");
    exit(2);
  }
  return shmid;
}

int CreateShm()//创建方
{
  return GetShareMemHelper(IPC_CREAT | IPC_EXCL | 0666);
}

int GetShm()//获取方
{
  return GetShareMemHelper(IPC_CREAT);
}


